package com.dahua.messagesyncsh.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class Control56 {
    private Long id;
    private Integer stationid;
    //  时间 格式如：2019-07-07 14:57:00.001  精确到毫秒
    private Date time;
    private Integer equipmentid;
    private String statuscode;
    private String faultcode;
    private Integer mqsendstatus;
    private Integer serversendstatus;

    // 通力或者其他品牌用
    private Integer code1;
    private Integer code2;
    private Integer code3;
    private Integer code4;
    private Integer code5;
    private Integer code6;
    private Integer code7;
    private Integer code8;
    private Integer code9;
    private Integer code10;
    private Integer code11;
    private Integer code12;
    private Integer code13;
    private Integer code14;
    private Integer code15;
    
    Control56(T485 t485, EquipmentInfo enquire){
        this.id = t485.getDataTime();
        this.stationid = enquire.getStationId();
        this.equipmentid = enquire.getId();
        this.time = new Date(t485.getDataTime());
        this.mqsendstatus  = 0;
        this.serversendstatus = 0;
        for (Integer[] array : t485.getData()) {
            //  暂时使用这几个
            switch (array[0]) {
                case 40001:
                    this.code1=array[1];
                    break;
                case 40002:
                    this.code2=array[1];
                    break;
                case 40003:
                    this.code3=array[1];
                    break;
                case 40004:
                    this.code4=array[1];
                    break;
                case 40005:
                    this.code5=array[1];
                    break;
                case 40006:
                    this.code6=array[1];
                    break;
                case 40007:
                    this.code7=array[1];
                    break;
                case 40008:
                    this.code8=array[1];
                    break;
                case 40009:
                    this.code9=array[1];
                    break;
                case 40010:
                    this.code10=array[1];
                    break;
                case 40011:
                    this.code11=array[1];
                    break;
                case 40012:
                    this.code12=array[1];
                    break;
                case 40013:
                    this.code13=array[1];
                    break;
                case 40014:
                    this.code14=array[1];
                    break;
                case 40015:
                    this.code15=array[1];
            }
        }
    }


    public static boolean isEquals(Control56 history, T485 t485) {

        if (t485.getDataTime() <= history.getTime().getTime()) {
            return true;
        }

        if (t485.getDataTime() > history.getTime().getTime()) {
            return false;
        }

        LocalDate today = LocalDate.now();
        int year = today.getYear();
        int month = today.getMonthValue();
        int day = today.getDayOfMonth();

        ZonedDateTime specificTime = ZonedDateTime.of(year, month, day, 1, 0, 0, 0, ZoneId.of("Asia/Shanghai"));
        /*  当天一点的时间戳*/
        long epochMilli = specificTime.toInstant().toEpochMilli();

        /*  如果history数据是一天前的数据则更新*/
        if (history.getTime().getTime() < epochMilli) {
            return false;
        }

        boolean res = true;

        for (Integer[] array : t485.getData()) {
            switch (array[0]) {
                case 40001:
                    res = history.getCode1() != null && history.getCode1().equals(array[1]);
                    if (!res) {
                        return false;
                    }
                    break;
                case 40002:
                    res = history.getCode2() != null && history.getCode2().equals(array[1]);
                    if (!res) {
                        return false;
                    }
                    break;
                case 40003:
                    res = history.getCode3() != null && history.getCode3().equals(array[1]);
                    if (!res) {
                        return false;
                    }
                    break;
                case 40004:
                    res = history.getCode4() != null && history.getCode4().equals(array[1]);
                    if (!res) {
                        return false;
                    }
                    break;
                case 40005:
                    res = history.getCode5() != null && history.getCode5().equals(array[1]);
                    if (!res) {
                        return false;
                    }
                    break;
                case 40006:
                    res = history.getCode6() != null && history.getCode6().equals(array[1]);
                    if (!res) {
                        return false;
                    }
                    break;
                case 40007:
                    res = history.getCode7() != null && history.getCode7().equals(array[1]);
                    if (!res) {
                        return false;
                    }
                    break;
                case 40008:
                    res = history.getCode8() != null && history.getCode8().equals(array[1]);
                    if (!res) {
                        return false;
                    }
                    break;
                case 40009:
                    res = history.getCode9() != null && history.getCode9().equals(array[1]);
                    if (!res) {
                        return false;
                    }
                    break;
                case 40010:
                    res = history.getCode10() != null && history.getCode10().equals(array[1]);
                    if (!res) {
                        return false;
                    }
                    break;
                case 40011:
                    res = history.getCode11() != null && history.getCode11().equals(array[1]);
                    if (!res) {
                        return false;
                    }
                    break;
                case 40012:
                    res = history.getCode12() != null && history.getCode12().equals(array[1]);
                    if (!res) {
                        return false;
                    }
                    break;
                case 40013:
                    res = history.getCode13() != null && history.getCode13().equals(array[1]);
                    if (!res) {
                        return false;
                    }
                    break;
                case 40014:
                    res = history.getCode14() != null && history.getCode14().equals(array[1]);
                    if (!res) {
                        return false;
                    }
                    break;
                case 40015:
                    res = history.getCode15() != null && history.getCode15().equals(array[1]);
                    if (!res) {
                        return false;
                    }
                    break;
            }
        }

        return res;
    }


    public static Control56 convertToControl56(T485 t485, EquipmentInfo enquire) {
        Control56 control56 = Control56.builder()
                .id(t485.getDataTime())
                .stationid(enquire.getStationId())
                .equipmentid(enquire.getId())
                .time(new Date(t485.getDataTime()))
                .mqsendstatus(0)
                .serversendstatus(0)
                .build();

        for (Integer[] array : t485.getData()) {
            //  暂时使用这几个
            switch (array[0]) {
                case 40001:
                    control56.setCode1(array[1]);
                    break;
                case 40002:
                    control56.setCode2(array[1]);
                    break;
                case 40003:
                    control56.setCode3(array[1]);
                    break;
                case 40004:
                    control56.setCode4(array[1]);
                    break;
                case 40005:
                    control56.setCode5(array[1]);
                    break;
                case 40006:
                    control56.setCode6(array[1]);
                    break;
                case 40007:
                    control56.setCode7(array[1]);
                    break;
                case 40008:
                    control56.setCode8(array[1]);
                    break;
                case 40009:
                    control56.setCode9(array[1]);
                    break;
                case 40010:
                    control56.setCode10(array[1]);
                    break;
                case 40011:
                    control56.setCode11(array[1]);
                    break;
                case 40012:
                    control56.setCode12(array[1]);
                    break;
                case 40013:
                    control56.setCode13(array[1]);
                    break;
                case 40014:
                    control56.setCode14(array[1]);
                    break;
                case 40015:
                    control56.setCode15(array[1]);
            }
        }
        return control56;
    }
}
